package com.xinzhidi.web.netty.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.web.netty.entity.AgreementHeatHistory;
import com.xinzhidi.web.netty.entity.dto.AgreementHeatHistoryAddDto;
import com.xinzhidi.web.netty.entity.dto.AgreementHeatHistoryListDto;
import com.xinzhidi.web.netty.entity.dto.AgreementHeatHistoryUpdDto;
import com.xinzhidi.web.netty.entity.vo.AgreementHeatHistoryInfoVo;
import com.xinzhidi.web.netty.entity.vo.AgreementHeatHistoryListVo;
import com.xinzhidi.web.netty.mapper.AgreementHeatHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 * 户用、单元热量协议-历史数据Service业务层处理
 *
 * @author cx
 * @date 2023-06-17
 */
@Service
public class AgreementHeatHistoryService extends ServiceImpl<AgreementHeatHistoryMapper, AgreementHeatHistory> {
    @Autowired
    private AgreementHeatHistoryMapper agreementHeatHistoryMapper;

    /**
     * 查询户用、单元热量协议-历史数据
     *
     * @param id 户用、单元热量协议-历史数据主键
     * @return 户用、单元热量协议-历史数据
     */
    public AgreementHeatHistoryInfoVo selectAgreementHeatHistoryById(Long id) {
        AgreementHeatHistory agreementHeatHistory = agreementHeatHistoryMapper.selectById(id);
        AgreementHeatHistoryInfoVo infoVo = new AgreementHeatHistoryInfoVo();
        BeanUtils.copyProperties(agreementHeatHistory, infoVo);
        return infoVo;
    }

    /**
     * 查询户用、单元热量协议-历史数据列表
     *
     * @param dto 查询户用、单元热量协议-历史数据dto
     * @return 户用、单元热量协议-历史数据
     */
    public TableDataInfo<AgreementHeatHistoryListVo> selectAgreementHeatHistoryList(AgreementHeatHistoryListDto dto) {
        LambdaQueryWrapper<AgreementHeatHistory> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(AgreementHeatHistory::getId);
        return TableDataInfo.getDataTable(agreementHeatHistoryMapper.selectList(wrapper), (domain -> {
            AgreementHeatHistoryListVo vo = new AgreementHeatHistoryListVo();
            BeanUtils.copyProperties(domain, vo);
            return vo;
        }));
    }

    /**
     * 新增户用、单元热量协议-历史数据
     *
     * @param dto 新增户用、单元热量协议-历史数据dto
     * @return 结果
     */
    public int insertAgreementHeatHistory(AgreementHeatHistoryAddDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementHeatHistory agreementHeatHistory = new AgreementHeatHistory();
        BeanUtils.copyProperties(dto, agreementHeatHistory);
        agreementHeatHistory.setCreateBy(loginUser.getUserId().toString());
        agreementHeatHistory.setCreateTime(DateUtils.getNowDate());
        return agreementHeatHistoryMapper.insert(agreementHeatHistory);
    }

    /**
     * 修改户用、单元热量协议-历史数据
     *
     * @param dto 修改户用、单元热量协议-历史数据dto
     * @return 结果
     */
    public int updateAgreementHeatHistory(AgreementHeatHistoryUpdDto dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        AgreementHeatHistory agreementHeatHistory = new AgreementHeatHistory();
        BeanUtils.copyProperties(dto, agreementHeatHistory);
        agreementHeatHistory.setUpdateBy(loginUser.getUserId().toString());
        agreementHeatHistory.setUpdateTime(DateUtils.getNowDate());
        return agreementHeatHistoryMapper.updateById(agreementHeatHistory);
    }

    /**
     * 批量删除户用、单元热量协议-历史数据
     *
     * @param ids 需要删除的户用、单元热量协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementHeatHistoryByIds(Long[] ids) {
        return agreementHeatHistoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除户用、单元热量协议-历史数据信息
     *
     * @param id 户用、单元热量协议-历史数据主键
     * @return 结果
     */
    public int deleteAgreementHeatHistoryById(Long id) {
        return agreementHeatHistoryMapper.deleteById(id);
    }
}
